<?php
/**
 * Created by PhpStorm.
 * User: jocky
 * Date: 09/10/2017
 * Time: 12:41
 */
namespace backend\controllers;

use common\models\ar\User;
use common\models\ar\MoneyOrder;
use common\models\RechargeDao;
use common\models\ar\UserBrokers;
class RechargeController extends BackendController
{
    public function actionLists()
    {
        $req = \Yii::$app->request;
        $isRender = $req->get('isRender', 1);//1为搜索页面，2为异步获取代理记录
        $userModel = UserBrokers::findOne(\Yii::$app->user->identity->id);
        $status = (int)$req->get('status', -1);
        $clientSN = trim($req->get('clientSN', ''));
        $pClientSN = trim($req->get('p_clientSN', ''));
        $pageNo = (int)$req->get('pageNo', 1);
        $pageSize = \Yii::$app->params['pageSize'];
        //日期
        $from = $req->get('from', date('Ymd', time() - 7 * 24 * 3600));
        $to = $req->get('to', date('Ymd'));
        $from = strtotime($from);
        $to = strtotime($to) + 24 * 3600;
        $uid = -1;
        if ($clientSN != '') {
            $where['loginName'] = $clientSN;
            $tmp = User::find()->where($where)->one();
            if (!$tmp) {
                $uid = 0;
            } else {
                $uid = $tmp->userID;
            }
        }
        if ($pClientSN) {
            $where = [];
            if ($pClientSN != $userModel->loginName) {
                $where['loginName'] = $pClientSN;
            }
            if (!empty($where)) {
                $tmp = User::find()->where($where)->one();
                if (!$tmp) {
                    $pid = 0;
                } else {
                    $pid = $tmp->id;
                }
            } else {
                $pid = $userModel->id;
            }
        } else {
            $pid = $userModel->id;
        }

        //直接返回
        if ($uid == 0 || $pid == 0) {
            $return = [
                'pageNo' => $pageNo,
                'pageTotal' => 0,
                'sumMoney' => 0,
                'lists' => [],
            ];
        } else {
            $res = RechargeDao::history(
                $status,
                $uid,
                $from,
                $to,
                $pageNo,
                $pageSize
            );
            $return = [
                'pageNo' => $pageNo,
                'pageTotal' => ceil($res['total'] / $pageSize),
                'lists' => $res['lists'],
                'money'=>$res['moneys']
            ];
        }
        if ($isRender == 1) {
            $return['from'] = $from;
            $return['to'] = $to - 1;
            return $this->renderPartial('lists', $return);
        } else {
            exit(json_encode($return));
        }
    }

    public function actionDetail()
    {

        $uid = (int)\Yii::$app->request->get('id', 0);
        $obj = MoneyOrder::findOne(['id'=>$uid]);
        if (!$obj) {
            exit("用户不存在");
        }
        return $this->renderPartial('detail', [
            'uid' => $uid,
            'fields' => $obj,
        ]);
    }

    public function actionOperate()
    {
        $loginName = \Yii::$app->user->identity->loginName;
        $uid = \Yii::$app->request->get('money_order', 0);//用户ID
        $tmp = MoneyOrder::findOne(['money_order'=>$uid]);
        if (!$tmp || $tmp->user_type != 1) {
            exit(json_encode(['code' => 0, 'msg' => '状态已变化']));
        }

        $type = (int)\Yii::$app->request->get('type', 0);//0拒绝，1通过
        $errorMsg = \Yii::$app->request->get('error_msg', '');//拒绝原因
        if ($type=='1')
        {
            $tmp->user_type=2;
            $tmp->Explain=$loginName."同意通过";
            $tmp->save(false);
            $msgs="您已经确认此订单";
            $bool=true;
        }else{
            $tmp->user_type=3;
            $tmp->Explain=$errorMsg;
            $tmp->save(false);
            $msgs="您已经拒绝此订单";
            $bool=true;
        }
        if ($bool) {
            $code = 1;
            $msg = $msgs;
        } else {
            $code = 0;
            $msg = '操作失败';
        }

        exit(json_encode(['code' => $code, 'msg' => $msg]));
    }
}